#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=5e3+10;
int a[MAXN][3],len[3],head[30],cnt,vis[30],match[30],ans,pos,tot,tim;
bool chose[30];
struct edge
{
	int v,next,val;
}e[MAXN];
void addedge(int x,int y,int z)
{
	e[++cnt]=(edge){y,head[x],z};
	head[x]=cnt;
	return;
}
bool Hungary(int u)
{
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].v;
		if(!chose[e[i].val]&&vis[v]<tim)
		{
			vis[v]=tim;
			if(!match[v]||Hungary(match[v]))
			{
				match[v]=u;
				return true;
			}
		}
	}
	return false;
}
int cal(int sum)
{
	int ret=0;
	memset(match,0,sizeof(match));
	for(int i=1;i<=len[(pos+1)%3];++i)
	{
		++tim;
		if(Hungary(i))++ret;
		if(ret+sum>=ans)return ans;
	}
	return ret+sum;
}
void dfs(int dep,int sum)
{
	if(dep>len[pos])
	{
		ans=min(ans,cal(sum));
		return;
	}
	chose[dep]=false;
	dfs(dep+1,sum);
	chose[dep]=true;
	dfs(dep+1,sum+1);
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int T=read();
	while(T--)
	{
		for(int i=0;i<=2;++i)len[i]=read();
		ans=INF;cnt=tot=tim=0;
		memset(head,0,sizeof(head));
		memset(vis,0,sizeof(vis));
		if(len[0]<=len[1]&&len[0]<=len[2])pos=0;
		else if(len[1]<=len[0]&&len[1]<=len[2])pos=1;
		else pos=2;
		for(int i=1;i<=len[0];++i)
			for(int j=1;j<=len[1];++j)
				for(int k=1;k<=len[2];++k)
					if(read())a[++tot][0]=i,a[tot][1]=j,a[tot][2]=k;
		for(int i=1;i<=tot;++i)
		{
			if(!pos)addedge(a[i][1],a[i][2],a[i][0]);
			else if(pos==1)addedge(a[i][2],a[i][0],a[i][1]);
			else addedge(a[i][0],a[i][1],a[i][2]);
		}
		dfs(1,0);
		printf("%d\n",ans);
	}
	return 0;
}
